package br.armx.activities.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import br.armx.activities.fragments.list.ChamadosListActivity;
import br.armx.activities.fragments.list.ManuaisOfflineListFragment;
import br.armx.maxos.R;
import br.armx.util.EnumAcao;
import br.armx.util.EnumTpMsg;
import br.armx.util.Util;

public class TabFragment extends Fragment {

	private String tag = "maxos";
	private final Handler mHandler = new Handler();
	private Button btnDemandas, btnManuais;
	private static boolean paused = true;

	public void carregaAbaListaDemandas() {
		try {
			FragmentManager fm = getFragmentManager();
			if (fm != null) {
				btnDemandas
						.setBackgroundResource(R.drawable.aba_gradiente_verde_ativa);
				btnManuais
						.setBackgroundResource(R.drawable.aba_gradiente_verde_inativa);

				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.fragment_content, new ChamadosListActivity());
				ft.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void carregaAbaListaManuais() {
		try {
			btnDemandas
					.setBackgroundResource(R.drawable.aba_gradiente_verde_inativa);
			btnManuais
					.setBackgroundResource(R.drawable.aba_gradiente_verde_ativa);

			FragmentManager fm = getFragmentManager();
			if (fm != null) {
				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.fragment_content,
						new ManuaisOfflineListFragment());
				ft.commit();
			}
		} catch (Exception e) {
			Log.e(tag, e.getMessage());
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragmento_abas, container,
				false);
		try {
			btnDemandas = (Button) view.findViewById(R.id.v01_btnDemandas);
			btnManuais = (Button) view.findViewById(R.id.v01_btnManuais);

			btnDemandas.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					carregaAbaListaDemandas();
				}
			});

			view.findViewById(R.id.v01_btnManuais).setOnClickListener(
					new OnClickListener() {
						@Override
						public void onClick(View v) {
							carregaAbaListaManuais();
						}
					});

			carregaAbaListaDemandas();
		} catch (Exception e) {
			Log.e(tag, e.getMessage());
		}
		return view;
	}

	private final BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				if (intent != null || !isPaused()) {
					Bundle bundle = intent.getExtras();
					if (bundle != null) {
						String tipoMensagem = bundle.getString("tipoMensagem");
						if (tipoMensagem.equals("erro")) {
							String mensagem = bundle.getString("mensagem");
							Util.errorDlg(mensagem, mHandler, context);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				enviarBroadcast(e.getMessage(), EnumTpMsg.erro);
			}
		}
	};

	@Override
	public void onResume() {
		try {
			super.onResume();
			setPaused(false);
			IntentFilter filter = new IntentFilter();
			filter.addAction(EnumAcao.ACTION_ATUALIZA_DEMANDAS.toString());
			filter.addAction(EnumAcao.ACTION_ATUALIZA_PRODUTOS.toString());
			getActivity().registerReceiver(receiver, filter, null, mHandler);
		} catch (Exception e) {
			Util.errorDlg(e.getMessage(), mHandler, getActivity());
		}
	}

	@Override
	public void onPause() {
		try {
			super.onPause();
			setPaused(true);
			getActivity().unregisterReceiver(receiver);
		} catch (Exception e) {
			Util.errorDlg(e.getMessage(), mHandler, getActivity());
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IntentFilter filter = new IntentFilter();
		filter.addAction(EnumAcao.ACTION_ATUALIZA_DEMANDAS.toString());
		filter.addAction(EnumAcao.ACTION_ATUALIZA_PRODUTOS.toString());
		getActivity().registerReceiver(receiver, filter, null, mHandler);
	}

	private void enviarBroadcast(String mensagem, EnumTpMsg tipoMensagem) {
		Intent i = new Intent();
		i.setAction(String.valueOf(EnumAcao.ACTION_ATUALIZA_DEMANDAS.toString()));
		Bundle extras = new Bundle();
		extras.putString("tipoMensagem", tipoMensagem.toString());
		extras.putString("mensagem", mensagem);
		i.putExtras(extras);
		getActivity().sendBroadcast(i);
	}

	public static boolean isPaused() {
		return paused;
	}

	public static void setPaused(boolean paused) {
		TabFragment.paused = paused;
	}
}
